define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'insurance.section/index',
        add_url: 'insurance.section/add',
        edit_url: 'insurance.section/edit',
        delete_url: 'insurance.section/delete',
        export_url: 'insurance.section/export',
        modify_url: 'insurance.section/modify',
        qe_url: 'insurance.qe/index',

    };

    return {

        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh',
                    [{
                        text: '添加',
                        url: init.add_url,
                        method: 'open',
                        auth: 'add',
                        class: 'layui-btn layui-btn-normal layui-btn-sm',
                        icon: 'fa fa-plus ',
                        extend: 'data-width="90%" data-height="95%"',
                    }],
                    'delete', 
                    // 'export', 
                    // 'recycle'
                ],
                cols: [[
                    {type: "checkbox"},
                    {field: 'id', width: 80, title: 'ID', searchOp: '='},
                    {field: 'title', minWidth: 80, title: '险种名称'},
                    {field: 'is_car_damage', minWidth: 80, title: '是否有车损', search: 'select', selectList: {0: '否', 1: '是'}, search: false},
                    {field: 'type_sy', minWidth: 80, title: '是否有商业险', search: 'select', selectList: {0: '否', 1: '是'}, search: false},
                    {field: 'type_jq', minWidth: 100, title: '是否有交强险', search: 'select', selectList: {0: '否', 1: '是'}, search: false},
                    // {field: 'type_jc', minWidth: 100, title: '是否有驾乘险', search: 'select', selectList: {0: '否', 1: '是'}, search: false},
                    {field: 'status', title: '状态', width: 85, selectList: {1: '启用', 0: '禁用'}, templet: ea.table.switch},
                    // {field: 'create_time', minWidth: 80, title: '创建时间', search: 'range'},
                    {
                        width: 250,
                        title: '操作',
                        templet: ea.table.tool,
                        operat: [
                            [
                            {
                                text: '编辑',
                                url: init.edit_url,
                                method: 'open',
                                auth: 'edit',
                                class: 'layui-btn layui-btn-xs layui-btn-success',
                                extend: 'data-width="90%" data-height="95%"',
                            }],
                            'delete']
                    }
                ]],
            });

            ea.listen();
        },
        add: function () {
            layui.util.on({
                AiOptimization: function (data) {
                    let layOn = $(data).attr('lay-on')
                    $(data).attr('lay-on', layOn + 'Loading')
                    aiOptimization(data)
                },
            });
            
            layui.form.on('select(type_sy)', function(data){
                var elem = data.elem; // 获得 select 原始 DOM 对象
                var value = data.value; // 获得被选中的值
                var othis = data.othis; // 获得 select 元素被替换后的 jQuery 对象
                console.log(123)
                if (value == 1) {
                    $('#is_car_damage_item').removeClass('layui-hide');
                } else {
                    $('#is_car_damage_item').addClass('layui-hide');
                    $('#is_car_damage').val(1)
                    layui.form.render('select');
                }
            });

            ea.listen();
        },
        edit: function () {
            layui.util.on({
                AiOptimization: function (data) {
                    let layOn = $(data).attr('lay-on')
                    $(data).attr('lay-on', layOn + 'Loading')
                    aiOptimization(data)
                },
            })

            layui.form.on('select(type_sy)', function(data){
                var elem = data.elem; // 获得 select 原始 DOM 对象
                var value = data.value; // 获得被选中的值
                var othis = data.othis; // 获得 select 元素被替换后的 jQuery 对象
                console.log(123)
                if (value == 1) {
                    $('#is_car_damage_item').removeClass('layui-hide');
                } else {
                    $('#is_car_damage_item').addClass('layui-hide');
                    $('#is_car_damage').val(1)
                    layui.form.render('select');
                }
            });

            ea.listen();
        },
        stock: function () {
            ea.listen();
        },
        recycle: function () {
            init.index_url = init.recycle_url;
            
            ea.listen();
        },

        upload: function () {
            ea.listen();
        }
    };

    function aiOptimization(data) {
        let layOn = $(data).attr('lay-on')
        let title = $('input[name="title"]').val()

        // 告诉AI 你需要做什么
        let message = `优化这个标题 ${title}`

        if ($.trim(title) === '') {
            ea.msg.error('标题不能为空', function () {
                $(data).attr('lay-on', layOn.split('Loading')[0])
            })
            return false
        }
        let url = ea.url('insurance.pollicy/aiOptimization')
        ea.request.post({url: url, data: {message: message}}, function (res) {
            let content = res.data?.choices[0]?.message?.content
            // stream 为true 时，AI 内容会逐字输出
            let stream = true
            ea.ai.chat(content, {stream: stream}, function () {
                $(data).attr('lay-on', layOn.split('Loading')[0])
            })
        }, function (error) {
            ea.msg.error(error.msg, function () {
                $(data).attr('lay-on', layOn.split('Loading')[0])
            })
        })
    }
});